$(function($){
	initScroll();
	initPullRefresh();
	$(".fa-refresh").on("tap",function(){
		// 刷新页面
		mui("#refreshContainer").pullRefresh().pulldownLoading();
	});
	// edit编辑
	Edit();
	// 删除
	trash();
	// 计算金额
	$("body").on("change","[type='checkbox']",function(){
		totalSum(); 
	});

});

function initPullRefresh(){
	mui.init({
	  pullRefresh : {
	    container:"#refreshContainer",
	    down : {
	      auto:true,
	      callback :function(){
    		var that = this;
    		LT.loginAjax({
    			url:"/cart/queryCartPaging",
    			type:'get',
    			data:{
    				page:1,
    				pageSize: 20
    			},
    			dataType:'json',
    			success:function(res){
    				// 清除加载动画
    				setTimeout(function(){	
    					window.LT.cartData = res.data;
						that.endPulldownToRefresh();
    					$("#lt_pro_list").html(template("pro_list",res));
    					$(".loading").remove();
						that.refresh(true);
			      	},1000)
    			}
    		})
	      } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
	    },
	    up: {
	    	callback:function(){
	    		var that = this;
	    		setTimeout(function(){
				that.endPullupToRefresh(true);
		  		},1000);
	    	} 
	  	}
	  }
	});
}

function Edit(){
	$("#lt_pro_list").on('tap',".mui-icon-compose",function(){
		var id = $(this).parent().attr("data-id"); 
		var data = LT.getItemById(LT.cartData,id);
		var eidtHtml = template("confirm",data);
		maxNum = data.productNum;
		mui.confirm(eidtHtml.replace(/\n/g,""),"编辑商品",["确定","取消"],function(e){
			if(e.index == 0){
				// 发送ajax
				var size = $(".lt_size span.active").html();
				var num = $(".lt_number input").val();
				LT.loginAjax({
					url:"/cart/updateCart",
					type:"post",
					data:{
						id:id,
						size:size,
						num:num

					},
					dataType:"json",
					success:function(){
						mui("#refreshContainer").pullRefresh().pulldownLoading();
						totalSum();
					}
				})
			}
			else {

			}
		},'div')
	});


	$("body").on("tap",".btn_size",function(){
		$(this).addClass('active').siblings().removeClass('active');
	});

	$('body').on('tap','.subtract',function(){
		var num = $('.lt_number input').val();
		if(num > 0){
			num--;
			$('.lt_number input').val(num);
		}
	});
	$('body').on('tap','.plus',function(){
		var num = $('.lt_number input').val();
		var maxNum = $('.lt_number input').attr("data-max");
		if(parseInt(num) < parseInt(maxNum) ){
			num++;
			$('.lt_number input').val(num);
		}
		else {
			mui.toast('库存不足');
		}
	});
}

function trash(){
	$("body").on("tap",".mui-icon-trash",function(){
		var id = $(this).parent().attr('data-id');
		var that = $(this);
		mui.confirm("你确定要删除该商品吗?","温馨提示",["确定","取消"],function(e){
			if(e.index == 0){

				LT.loginAjax({
					url:"/cart/deleteCart",
					type:"get",
					dataType:"json",
					data:{
						id:[id]
					},
					success:function(){
						mui.toast('删除成功');
						that.parent().parent().remove();
						totalSum();
					}
				})
			}
			else
			{

			}
		},'div')

		
	})
}

function totalSum(){
	// 找到所有的checkbox被选中的
	var $checked = $("[type=checkbox]:checked");
	var amountSum = 0;
	$checked.each(function(){
		var id = $(this).attr("data-id");
		var item = LT.getItemById(LT.cartData,id);
		var num = item.num;
		var price = item.price;
		amountSum += num * price;
	})
	amountSum = Math.floor(amountSum * 100) / 100;
	if(amountSum % 10 == 0){
		amountSum = amountSum.toString() + '.00';
		$('.amoundNum').html(amountSum);
		return false;
	}
	$('.amoundNum').html(amountSum);
}